package com.mycontentprovider2;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    Uri parse_uri=Uri.parse("content://com.mycontentprovider1/student");
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
        setContentView(R.layout.activity_main);
        findViewById(R.id.insert)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ContentValues values = new ContentValues();
                        values.put("name","lilianjie");
                        values.put("number","10");
                        getContentResolver().insert(parse_uri,values);
                    }
                });
        findViewById(R.id.search)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try{
                            Cursor query = getContentResolver().query(parse_uri, null, null,null,null);
                            while (query.moveToNext()){
                                String string = query.getString(1);
                                String string1 = query.getString(2);
                                Log.i("===", "name=="+string+"  number=="+string1);
                                Toast.makeText(MainActivity.this,"name=="+string+"  number=="+string1,Toast.LENGTH_SHORT).show();
                            }}
                        finally {
//                            query.close();
                        }
                    }
                });
    }

    Uri mPersonQueryUri=Uri.parse("content://com.mycontentprovider1/persons");
    //开关 contentprovider
    public void btnSwitch(View view) {

        Cursor cursor = getContentResolver().query(mPersonQueryUri, null, null, null, null);
        Log.i(TAG, "btnSwitch: getresult");
        if (cursor != null && cursor.moveToFirst()) {
            int columnCount = cursor.getColumnCount();
            do{
                for (int i = 0; i < columnCount; i++) {
                    String string = cursor.getString(i);
                    Log.i(TAG, "cursor res: "+string);
                }
            } while (cursor.moveToNext());
        }

    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }
}
